#!/usr/bin/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start EmberZNet Zigbee Daemon
# ==============================================================================
# shellcheck disable=SC2015
mkdir -p /data/zigbeed && cd /data/zigbeed || bashio::exit.nok "Could not change to zigbeed working directory"

ezsp_listen="::"
ezsp_listen_port="$(bashio::addon.port 9999)"

# If user port is not set, listen on local interface only
if ! bashio::var.has_value "${ezsp_listen_port}"; then
    ezsp_listen="$(bashio::addon.ip_address)"
    ezsp_listen_port="9999"
fi

bashio::log.info "Starting zigbeed..."
# shellcheck disable=SC2086
exec /usr/local/bin/zigbeed --radio-url "spinel+cpc://cpcd_0?iid=1&iid-list=0" \
                           --ezsp-listen ${ezsp_listen} \
                           --ezsp-listen-port ${ezsp_listen_port}
